package com.ms.dsq_english.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

public class SecretUtils
{
    static String CODE = "ZWywbT9IUgEIoOfOvUfXl7KtaB110nAX";

    public static String byMD5Code(String msg)
    {
        MessageDigest md5 = null;

        try
        {
            md5 = MessageDigest.getInstance("MD5");

            md5.update(msg.getBytes(StandardCharsets.UTF_8));
        }
        catch (NoSuchAlgorithmException e) {e.printStackTrace();}

        byte[] encryption = Objects.requireNonNull(md5).digest();

        StringBuilder strBuf = new StringBuilder();

        for (byte b : encryption)
        {
            if (Integer.toHexString(0xff & b).length() == 1)
            {
                strBuf.append("0").append(Integer.toHexString(0xff & b));
            }
            else
            {
                strBuf.append(Integer.toHexString(0xff & b));
            }
        }

        return strBuf.toString();
    }
}
